//
//  ViewController.m
//  Demo0037
//
//  Created by zTercel on 13-4-11.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
static NSString *cellIdentifier = @"cellIdentifier";
@synthesize names;
@synthesize keys;

NSComparisonResult (^compare)(id, id) = ^(id obj1, id obj2)
{
    return [obj1 compare:obj2 options:NSCaseInsensitiveSearch];
};

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    UITableView *tableView  = (id)[self.view viewWithTag:1];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
    
    NSString    *plist  = [[NSBundle mainBundle]pathForResource:@"sortednames" ofType:@"plist"];
    self.names          = [NSDictionary dictionaryWithContentsOfFile:plist];
    self.keys           = [[self.names allKeys]sortedArrayUsingComparator:compare];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark-
#pragma mark data source of table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return[[names allKeys]count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return keys[section];
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"footer";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString* key = keys[section];

    return [names[key] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    NSString*   section     = keys[indexPath.section];
    NSInteger   row         = indexPath.row;
    cell.textLabel.text     = names[section][row];
    return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return keys;
}


@end
